using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using Microsoft.EnterpriseSingleSignOn.Interop;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


namespace BizTalk.Tools.SSOStorage
{
    public static class SSOConfigManager
    {
        static DataSet dt;
        static SSOConfigManager()
		{
            //if (null == ssoAppList)
            //    ssoAppList = new SSOAppList();
		}

        

        //don't actually need a GUID value
        //private static string idenifierGUID = "ConfigProperties";

        // Tracks the list of registered applications
        //private static SSOAppList ssoAppList;


        /// <summary>
        /// Creates a new SSO ConfigStore application.
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="description"></param>
        /// <param name="uAccountName"></param>
        /// <param name="adminAccountName"></param>
        /// <param name="propertiesBag"></param>
        /// <param name="maskArray"></param>
        public static void CreateConfigStoreApplication(string appName, string description, 
            string uAccountName, 
            string adminAccountName, 
            SSOPropBag propertiesBag, 
            string contactInfo)
        {
            int appFlags = 0;

            //bitwise operation for flags
            appFlags |= SSOFlag.SSO_FLAG_APP_CONFIG_STORE;
            appFlags |= SSOFlag.SSO_FLAG_SSO_WINDOWS_TO_EXTERNAL;
            appFlags |= SSOFlag.SSO_FLAG_APP_ALLOW_LOCAL;

            ISSOAdmin ssoAdmin = new ISSOAdmin();

            //create app            
            ssoAdmin.CreateApplication(appName, description, contactInfo, uAccountName, adminAccountName, appFlags, propertiesBag.PropertyCount);

            //create property fields
            int counter = 0;

            //create dummy field in first slot
            ssoAdmin.CreateFieldInfo(appName, "dummy", 0);
            //create real fields
            foreach (DictionaryEntry de in propertiesBag.properties)
            {
                string propName = de.Key.ToString();
                int fieldFlags = 0;
                fieldFlags |= Convert.ToInt32(0);

                //create property
                ssoAdmin.CreateFieldInfo(appName, propName, fieldFlags);

                counter++;
            }

            //enable application
            ssoAdmin.UpdateApplication(appName, null, null, null, null, SSOFlag.SSO_FLAG_ENABLED, SSOFlag.SSO_FLAG_ENABLED);
        }


        public static void UpdateConfigStoreApplication(string appName, string description,
        string uAccountName,
        string adminAccountName,
        SSOPropBag propertiesBag,
        string contactInfo)
        {
            int appFlags = 0;

            //bitwise operation for flags
            appFlags |= SSOFlag.SSO_FLAG_APP_CONFIG_STORE;
            appFlags |= SSOFlag.SSO_FLAG_SSO_WINDOWS_TO_EXTERNAL;
            appFlags |= SSOFlag.SSO_FLAG_APP_ALLOW_LOCAL;

            ISSOAdmin ssoAdmin = new ISSOAdmin();
            //create property fields
            int counter = 0;
            
            //create real fields
            foreach (DictionaryEntry de in propertiesBag.properties)
            {
                string propName = de.Key.ToString();
                int fieldFlags = 0;
                fieldFlags |= Convert.ToInt32(0);

                //create property
                try
                {
                    ssoAdmin.CreateFieldInfo(appName, propName, fieldFlags);
                }
                catch { }
                
                counter++;
            }
            
            //enable application
            ssoAdmin.UpdateApplication(appName, null, null, null, null, SSOFlag.SSO_FLAG_ENABLED, SSOFlag.SSO_FLAG_ENABLED);
        }
        public static void deleteConfigProperties(string appName, string identifier)
        {
            ISSOConfigStore configStore = new ISSOConfigStore();

            configStore.DeleteConfigInfo(appName, identifier);
            
        }

        /// <summary>
        /// Set values for application fields
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="propertyBag"></param>
        public static void SetConfigProperties(string appName, string identifier, SSOPropBag propertyBag)
        {
            ISSOConfigStore configStore = new ISSOConfigStore();

            configStore.SetConfigInfo(appName, identifier, propertyBag);
 
        }

        /// <summary>
        /// Retrieve dictionary of field/value pairs
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="description"></param>
        /// <param name="contactInfo"></param>
        /// <param name="appUserAcct"></param>
        /// <param name="appAdminAcct"></param>
        /// <returns></returns>
        /// 
        public static HybridDictionary GetConfigProperties(string appName, string identifier, out string description, out string contactInfo, out string appUserAcct, out string appAdminAcct)
        {
            int flags;
            int count;
            HybridDictionary hd = new HybridDictionary();
            //get config info
            ISSOAdmin ssoAdmin = new ISSOAdmin();
            ssoAdmin.GetApplicationInfo(appName, out description, out contactInfo, out appUserAcct, out appAdminAcct, out flags, out count);
            
            //get properties
            ISSOConfigStore configStore = new ISSOConfigStore();
            SSOPropBag propertiesBag = new SSOPropBag();

            configStore.GetConfigInfo(appName, identifier, SSOFlag.SSO_FLAG_RUNTIME, propertiesBag);                        
            
            return propertiesBag.properties;
        }

        internal static  bool IsConfigExists(string appName, string identifier)
        {

            string appUserAcct, appAdminAcct, description, contactInfo;

            string ConnecitonString = ConfigurationManager.AppSettings["ConnecitonString"];
            HybridDictionary props = null;
            try { props = SSOConfigManager.GetConfigProperties(appName, identifier, out description, out contactInfo, out appUserAcct, out appAdminAcct); 
            }
            catch
            { return false; }
            if (null == props) return false;

            return props.Count > 0;

        }

        /// <summary>
        /// Remove the application
        /// </summary>
        /// <param name="appName"></param>
        public static void DeleteApplication(string appName)
        {
            ISSOAdmin ssoAdmin = new ISSOAdmin();

            ssoAdmin.DeleteApplication(appName);
        }

        //BEN
        public static bool ApplicationExists(string appName)
        {            
            string aDescription = string.Empty;
            string uAccountName= string.Empty;
            string adminAccountName= string.Empty;
            string adminGroupName = string.Empty;
            string dContactInfo = string.Empty;
            int flags = 0;
            int numFields = 0;
            int appFlags = 0;

            //bitwise operation for flags
            appFlags |= SSOFlag.SSO_FLAG_APP_CONFIG_STORE;
            appFlags |= SSOFlag.SSO_FLAG_SSO_WINDOWS_TO_EXTERNAL;
            appFlags |= SSOFlag.SSO_FLAG_APP_ALLOW_LOCAL;

            ISSOAdmin ssoAdmin = new ISSOAdmin();

            //create app            
            ssoAdmin.GetApplicationInfo(appName, out aDescription, out dContactInfo, out uAccountName, out adminGroupName, out flags, out numFields);

            try
            {
                string appUserAcct, appAdminAcct, description, contactInfo;
                SSOConfigManager.GetConfigProperties(appName, "ConfigProperties", out description, out contactInfo, out appUserAcct, out appAdminAcct);
            }
            catch
            {
                return false;
            }

            return true;
        }

        public static bool ApplicationExists(string appName, string identifier)
        {
            try
            {
                string appUserAcct, appAdminAcct, description, contactInfo;
                SSOConfigManager.GetConfigProperties(appName, identifier, out description, out contactInfo, out appUserAcct, out appAdminAcct);
            }
            catch
            {
                return false;
            }

            return true;
        }

        //BEN
        public static HybridDictionary ApplicationList
        {
            get { return SSOAppList.Instance.GetAppList(); }
        }
    }

    public class SSOPropBag : IPropertyBag
    {
        internal HybridDictionary properties;

        public SSOPropBag()
        {
            properties = new HybridDictionary();
        }

        #region IPropertyBag Members

        public void Read(string propName, out object ptrVar, int errorLog)
        {
            ptrVar = properties[propName];
        }

        public void Write(string propName, ref object ptrVar)
        {
            properties.Add(propName, ptrVar);
        }

        public int PropertyCount
        {
            get
            {
                return properties.Count;
            }
        }

        #endregion
    }
}
